﻿using AutoMapper;
using Newtonsoft.Json;
using System.Collections.Generic;
using housingsafety.server.Models;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using System;
using housingsafety.server.Security;
using Microsoft.AspNetCore.Http;

namespace housingsafety.server
{
    /// <summary>
    /// 实体映射关系
    /// </summary>
    public class AutoMapperProfiles : Profile
    {
        /// <summary>
        /// 添加你的实体映射关系
        /// </summary>
        public AutoMapperProfiles()
        {
            //User转UserDto.
            CreateMap<ZmUser, UserDto>()
                .ForMember(nameof(UserDto.District), opt
                => opt.MapFrom(src => JsonConvert.DeserializeObject<SimpleDepartmentDto>(src.District)))
                //映射发生之前
                .BeforeMap((source, dto) =>
                {

                })
                //映射发生之后
                .AfterMap((source, dto) =>
                {
                    if (!string.IsNullOrEmpty(source.Encphone))
                    {
                        dto.Phone = AesHelper.Decrypt(source.Encphone);
                    }

                    if (source.RoleType.HasValue)
                    {
                        dto.Role = (PermissionRoleType)source.RoleType;
                    }
                    else
                    {
                        dto.Role = PermissionRoleType.Admin;
                    }

                    if (source.Group.HasValue)
                    {
                        dto.UserGroup = (UserGroupType)source.Group;
                    }
                    else
                    {
                        dto.UserGroup = UserGroupType.Unknown;
                    }

                    if (source.Jurisdiction != null)
                    {
                        dto.JurisdictionCodes = JsonConvert
                        .DeserializeObject<List<SimpleDepartmentDto>>(source.Jurisdiction);
                    }
                });
            CreateMap<UserDto, ZmUser>()
                //映射发生之前
                .BeforeMap((dto, source) =>
                {
                    //code ...
                })
                //映射发生之后
                .AfterMap((dto, source) =>
                {
                    source.Phone = $"{dto.Phone.Substring(0, 3)}****{dto.Phone.Substring(7, 4)}";
                    source.RoleType = Convert.ToInt16(JsonConvert.SerializeObject(dto.Role));
                });
            CreateMap<ZmSystemLog, SystemLogDto>()
                .ForMember(nameof(SystemLogDto.RespRet), opt
                => opt.MapFrom(src => JsonConvert.DeserializeObject<RespRet>(src.Respret)))
                ;
            CreateMap<ZmDepartment, DepartmentDto>();




            CreateMap<ZmPcBase, ZmPcBaseLog>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {
               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {

               });

            CreateMap<ZmPcBaseCj, ZmPcBaseLogCj>()
              //映射发生之前
              .BeforeMap((source, dto) =>
              {
              })
              //映射发生之后
              .AfterMap((source, dto) =>
              {
                  dto.Townname = source.Towndm;
                  dto.Villagename = source.Villagedm;
                  dto.Xzqname = source.Xzq;
              });

            CreateMap<ZmPcBaseSqjz, ZmPcBaseLogSqjz>()
                 //映射发生之前
                 .BeforeMap((source, dto) =>
                 {
                 })
                 //映射发生之后
                 .AfterMap((source, dto) =>
                 {
                     dto.Townname = source.Towndm;
                     dto.Villagename = source.Villagedm;
                     dto.Xzqname = source.Xzq;
                 });

            CreateMap<ZmPcYhLogDx, ZmPcYhlogDxzgson>()
                        //映射发生之前
                        .BeforeMap((source, dto) =>
                        {
                        })
                        //映射发生之后
                        .AfterMap((source, dto) =>
                        {
                            dto.YhlogDxId = source.Id;
                            dto.Id = Guid.NewGuid().ToString();
                            dto.ZgBy = source.CheckBy;
                            dto.Zglx = source.Jcly.ToString();
                            dto.ZgTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            dto.Fwbh = source.Fwbh;
                            
                        });

            CreateMap<ZmPcYhLogDxCj, ZmPcYhlogDxzgsonCj>()
                     //映射发生之前
                     .BeforeMap((source, dto) =>
                     {
                     })
                     //映射发生之后
                     .AfterMap((source, dto) =>
                     {
                         dto.YhlogDxId = source.Id;
                         dto.Id = Guid.NewGuid().ToString();
                         dto.ZgBy = source.CheckBy;
                         dto.Zglx = source.Jcly.ToString();
                         dto.ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                         dto.Fwbh=source.Fwbh;
                     });
            CreateMap<ZmPcYhLogDxSqjz, ZmPcYhlogDxzgsonSqjz>()
                   //映射发生之前
                   .BeforeMap((source, dto) =>
                   {
                   })
                   //映射发生之后
                   .AfterMap((source, dto) =>
                   {
                       dto.YhlogDxId = source.Id;
                       dto.Id = Guid.NewGuid().ToString();
                       dto.ZgBy = source.CheckBy;
                       dto.Zglx = source.Jcly.ToString();
                       dto.ZgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                       dto.Fwbh = source.Fwbh;
                   });

            CreateMap<ZmPcYhLogDxZgDto, ZmPcYhLogDxZgDto>()
                     //映射发生之前
                     .BeforeMap((source, dto) =>
                     {
                     })
                     //映射发生之后
                     .AfterMap((source, dto) =>
                     {
                         if (DateTime.TryParse(source.FhTime, out DateTime dateTime))
                         {
                             dto.FhTime = dto.FhTime == null ? "" : String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Parse(source.FhTime));
                         }
                         if (DateTime.TryParse(source.ZgTime, out  dateTime))
                         {
                             dto.ZgTime = dto.ZgTime == null ? "" : String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Parse(source.ZgTime));
                         }
                         
                     });

            CreateMap<ZmPcYhLogDxDto, ZmPcYhLogDxDto2>()
                   //映射发生之前
                   .BeforeMap((source, dto) =>
                   {
                   })
                   //映射发生之后
                   .AfterMap((source, dto) =>
                   {
                       if (source.CheckTime != null)
                       {
                           dto.CheckTime=source.CheckTime.ToString("yyyy-MM-dd HH:mm:ss");
                       }
                   });
           
                  CreateMap<ZmPcYhLogCjDto, ZmPcYhLogCjDto2>()
                   //映射发生之前
                   .BeforeMap((source, dto) =>
                   {
                   })
                   //映射发生之后
                   .AfterMap((source, dto) =>
                   {
                       //if (source.CreateOn != null)
                       //{
                       //    dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd hh:mm:ss");
                       //}
                       //dto.Pczt = source.Pczt == 1 ? "无隐患" : "有隐患";
                   });

            CreateMap<ZmPcYhLogAllDto, ZmPcYhLogAllDto2>()
                //映射发生之前
                .BeforeMap((source, dto) =>
                {
                })
                //映射发生之后
                .AfterMap((source, dto) =>
                {
                    if (source.CreateOn != null)
                    {
                        dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    dto.Pczt = source.Pczt == 1 ? "无隐患" : "有隐患";
                });


            CreateMap<ZmPcYh, ZmPcYhLog>()
            //映射发生之前
            .BeforeMap((source, dto) =>
            {
            })
            //映射发生之后
            .AfterMap((source, dto) =>
            {
                dto.PcYhId = source.Id;
            });

            CreateMap<ZmPcYhCj, ZmPcYhLogCj>()
          //映射发生之前
          .BeforeMap((source, dto) =>
          {
          })
          //映射发生之后
          .AfterMap((source, dto) =>
          {
              dto.PcYhId = source.Id;
          });

            CreateMap<ZmPcYhSqjz, ZmPcYhLogSqjz>()
        //映射发生之前
        .BeforeMap((source, dto) =>
        {
        })
        //映射发生之后
        .AfterMap((source, dto) =>
        {
            dto.PcYhId = source.Id;
        });

            CreateMap<FwlbDto, FwlbDto>()
           //映射发生之前
           .BeforeMap((source, dto) =>
           {
           })
           //映射发生之后
           .AfterMap((source, dto) =>
           {
               if (dto.Pczt == null)
               {
                   dto.Pczt = 0;
               }
               if (dto.Zczt == null)
               {
                   dto.Zczt = 0;
               }

               if (dto.Jczt == null)
               {
                   dto.Jczt = 0;
               }
           });


            CreateMap<ZmPcGjzzCj, ZmPcGjzzCjLog>()
        //映射发生之前
        .BeforeMap((source, dto) =>
        {
        })
        //映射发生之后
        .AfterMap((source, dto) =>
        {
        });

            CreateMap<ZmPcGjzzCjDto, ZmPcGjzzCj>()
       //映射发生之前
       .BeforeMap((source, dto) =>
       {
       })
       //映射发生之后
       .AfterMap((source, dto) =>
       {
           dto.Pcsj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
       });



            CreateMap<ZmBuildings, ZmPcBase>()
           //映射发生之前
           .BeforeMap((source, dto) =>
           {
           })
           //映射发生之后
           .AfterMap((source, dto) =>
           {
           });


            CreateMap<ZmBuildings, ZmPcBaseSqjz>()
           //映射发生之前
           .BeforeMap((source, dto) =>
           {
           })
           //映射发生之后
           .AfterMap((source, dto) =>
           {
           });


            CreateMap<ZmBuildings, ZmPcBaseCj>()
           //映射发生之前
           .BeforeMap((source, dto) =>
           {
           })
           //映射发生之后
           .AfterMap((source, dto) =>
           {
           });

            CreateMap<ZmPcBase, ZmPcBaseCj>()
        //映射发生之前
        .BeforeMap((source, dto) =>
        {
        })
        //映射发生之后
        .AfterMap((source, dto) =>
        {
        });


            CreateMap<ZmPcBase, ZmPcBaseSqjz>()
       //映射发生之前
       .BeforeMap((source, dto) =>
       {
       })
       //映射发生之后
       .AfterMap((source, dto) =>
       {
       });
        }
        private Dictionary<string, string> JsonStr2Dict(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
            }
            catch (System.Exception)
            {
            }

            return null;
        }
        private List<string> JsonStr2List(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<List<string>>(jsonStr);
            }
            catch (System.Exception)
            {
            }
            return null;
        }
    }
}
